import {saveToLocal, loadFromLocal} from './apiLocalStorage'
// import * as _ from 'lodash'

const SEARCH_HISTORY = 'SEARCH_HISTORY'

export function loadSearchHistory (def = []) {
  return loadFromLocal(SEARCH_HISTORY, def)
}

export function saveSearchHistory (p) {
  if (!p) return
  // if (!(p instanceof String)) return
  p = p.trim()
  let searchHistory = loadFromLocal(SEARCH_HISTORY, [])
  let ind = searchHistory.indexOf(p)
  if (ind > -1) {
    searchHistory.splice(ind, 1)
  }
  searchHistory.unshift(p)
  if (searchHistory.length > 10) {
    searchHistory = searchHistory.slice(0, 10)
  }
  saveToLocal(SEARCH_HISTORY, searchHistory)
  return searchHistory
}
